<?php

abstract class Base {

        private static $instance;
        protected $id = null;
        protected $_table = null;

	/**
	 * Conexão com o banco de dados
	 * @var PDO 
	 */
	protected $db;
	
	public function __construct(array $options = null)
	{
		//$this->db = new PDO($dsn, $username, $passwd, $options);
                if(count($options))
                {
                    $this->setOptions($options);
                }
                 $this->db = $this->getConnection();


	}

        public function setOptions(array $options)
        {
            $methods = get_class_methods($this);
            foreach($options as $key => $value)
            {
                $method = 'set' . ucfirst($key);
                if(in_array($method, $methods))
                    $this->$method($value);
            }
            return $this;
        }

      public function getConnection() {
            if(!isset(self::$instance)){
                //$driver_options = array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' );
                try {
                    self::$instance  = new PDO(DB_DSN,DB_USER,DB_PASS,$driver_options);
                }
                catch(PDOException $e) {
                    die($e->getMessage());
                }
            }
            return self::$instance;
      }
      
        /**
        * @filesource
        * @return void
        */

        public function getId(){
            return $this->id;
        }
        
        /**
        * @filesource
        * @return void
        */
        
        public function setId($id){
            if(!is_null($this->id)){
                throw new exception('O ID não pode ser alterado');
            }
            $this->id = (int)$id;
        }

        /**
        * @filesource
        * @return void
        */

        public function fetchAll()
        {
            $db = $this->db;
            $stm = $db->prepare("SELECT * FROM ". $this->_table);
            $stm->execute();
            return $stm->fetchAll(PDO::FETCH_ASSOC);
        }

        /**
        * @filesource
        * @return void
        */

        public function find()
        {
            $db = $this->db;
            $stm = $db->prepare("SELECT * FROM ". $this->_table. " WHERE id=:id");
            $stm->bindValue(":id", $this->getId());
            $stm->execute();
            return $stm->fetch(PDO::FETCH_ASSOC);

        }

        public function delete()
        {
            //delete o registro onde id = $this->id
            $db = $this->db;
            $stm = $db->prepare("DELETE FROM ". $this->_table . " WHERE id=:id");
            $stm->bindValue(":id",$this->getId());
            return $stm->execute();
        }


        abstract protected function _insert();
        abstract protected function _update();
	
        public function save()
        {
            if(is_null($this->getId()))
            {
                $res = $this->_insert();

                if($res)
                {
                    $this->setId($this->getDb()->lastInsertId());
                    return $this->getId();
                }
            }
            else
            {
                return $this->_update();
            }
        }


        public function getDb(){
            return $this->db;
        }


            
}

?>
